﻿package com.cilico.single.utils;


import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View;

/**
 * 字符串
 * 
 * @author lv.xiangan
 *
 */
public final class StringTicker implements Runnable {
	public static StringTicker instance_;
	boolean running_ = true;
	int start_;
	int speed_;
	int x_;
	int y_;
	int width_;
	int height_;
	boolean waiting_ = true;
	long timeout_;
	Thread thread_;
	final static int FLPS = 8000;

	int getStart() {
		return start_;
	}

	void setStart(int start) {
		start_ = start;
	}

	void scrollRunOver() {
		running_ = false;
		thread_ = null;
		instance_ = null;
	}

	void initTicker(int start, int speed, int x, int y, int width, int height) {
		start_ = start;
		speed_ = speed;
		x_ = x;
		y_ = y;
		width_ = width;
		height_ = height;
	}

	void notifyTicker() {
		synchronized (this) {
			waiting_ = false;
			running_ = true;
			if (thread_ == null || !thread_.isAlive()) {
				thread_ = new Thread(this);
				thread_.start();
			} else {
				this.notify();
			}
		}
	}

	public void pauseTicker() {
		start_ = 0;
		waiting_ = true;
	}

	boolean isPause() {
		return waiting_;
	}

	public void run() {
		while (running_) {
			try {
				synchronized (this) {
					if (waiting_) {
						timeout_ = System.currentTimeMillis();
						wait(FLPS);
						timeout_ = System.currentTimeMillis() - timeout_;
						if (timeout_ >= FLPS) {
							running_ = false;
							waiting_ = false;
							thread_ = null;
							break;
						}
					}
				}
				Thread.sleep(speed_);
			} catch (InterruptedException ex) {
				// LPUtils.printException(ex);
			}
			if (running_) { // we should not repaint if running is false
				// if (null != ConfigManager.currentView_)
				// if (null != ConfigManager.currentView_.tabBar_)
				// if (null != ConfigManager.currentView_.tabBar_.llV_)
				// ConfigManager.currentView_.tabBar_.llV_.postInvalidate();
			}
		}
	}

	public static StringTicker instance() {
		if (instance_ == null) {
			instance_ = new StringTicker();
		}
		return instance_;
	}

	public void drawMoveStr(View view, Canvas g, int width, Paint font, int color, String str, int x, int y) {
		font.setColor(color);
		int strWidth = (int) font.measureText(str);
		if (strWidth <= width) {
			g.drawText(str, x, y, font);
			pauseTicker();
		} else {
			if (isPause() || thread_ == null || !running_) {
				initTicker(0, 200, x, y, width, (int) font.getTextSize());
				notifyTicker();
			}
			g.clipRect(x, y - font.getTextSize(), width, ((int) font.getTextSize()) << 1);
			g.drawText(str, x - getStart(), y, font);
			int space = (int) (font.measureText("w") * 3);
			if (getStart() >= strWidth - (width - space))
				g.drawText(str, x - getStart() + strWidth + space, y, font);
			g.clipRect(0, 0, Utils.screenWidth_, Utils.screenHeight_);
			// move 3 pels every step.
			if (getStart() <= strWidth - 1)
				setStart(getStart() + 1);
			else
				setStart(-space);
			view.postInvalidate();
		}
	}

}
